<?php
class C_mapping extends CI_Controller {
	function __construct(){
		parent::__construct();
		checkLogin();
		$this->load->model('m_mapping');
		$this->load->model('m_monitor');
		$this->load->model('m_harsat');
	}
	function index(){
		$task = secure_post('task');
		switch($task){
			case 'LIST' :
				$this->_list();
				break;
			case 'UPDATE2' :
				$this->_update2();
				break;
			case 'UPDATE' :
				$this->_update();
				break;
			case 'DELETE' :
				$this->_delete();
				break;
			case 'DETAIL' :
				$this->_detail();
				break;
			case 'DEACTIVATE' :
				$this->_deactivate();
				break;
			default :
                $data["list"] = $this->m_mapping->_list();
                $data["proyek"] = $this->m_public_function->_proyek();
				$this->load->view('app/v_mapping',$data);
		}
	}
	
	function _list(){
		$data['page'] = numeric_post("page");
		$data['rows'] = numeric_post("rows");
		$data['search'] = nosql_post("search");
		$data['m_proyek_id'] = nosql_post("m_proyek_id");
		$data['m_mandor_id'] = nosql_post("m_mandor_id");
		$data['monitor_periode'] = nosql_post("monitor_periode");
		$data['m_pembayaran_id'] = nosql_post("m_pembayaran_id");
		$data['m_pekerjaan_id'] = nosql_post("m_pekerjaan_id");
		$data['monitor_mulai'] = nosql_post("monitor_mulai");
		$data['monitor_selesai'] = nosql_post("monitor_selesai");
		$data['monitor_wakil'] = nosql_post("monitor_wakil");
		$data['monitor_tukang'] = nosql_post("monitor_tukang");
		$data['monitor_pekerja'] = nosql_post("monitor_pekerja");
		$data['monitor_koordinasi'] = nosql_post("monitor_koordinasi");
		$data['monitor_quality'] = nosql_post("monitor_quality");
		$data['monitor_safety'] = nosql_post("monitor_safety");
		$data['monitor_hk'] = nosql_post("monitor_hk");
		echo $this->m_monitor->_list($data);
	}
	
	function _update2(){
		$monitor_id = $this->input->post('monitor_id');
        $mulai = date("Y-m-d",strtotime($this->input->post('start')));
        $selesai = date("Y-m-d",strtotime($this->input->post('end')));
        
		$data = array(
			'mapping_tglmulai' => $mulai,
			'mapping_tglselesai' => $selesai
        );
        
		$this->m_mapping->_update2($data,$monitor_id);
		echo 1;
	}
	
	function _update(){
		$monitor_id = $this->input->post('monitor_id');
        
		$data = array(
			'm_proyek_id' => $this->input->post('m_proyek_id')
        );
        
		$this->m_mapping->_update($data,$monitor_id);
		echo 1;
 	}
	
	function _delete(){
		$monitor_id= $this->input->post('monitor_id');
		$this->m_monitor->_delete($monitor_id);
		echo 1;
	}
	
	function _deactivate(){
		$monitor_id= $this->input->post('monitor_id');
		$data = array(
			'monitor_active' => 'n'
		);
		$this->m_monitor->_deactivate($data,$monitor_id);
		echo 1;
	}
    
    function _insertdetail($monitor_id){
        
        $harsat_ket = $this->input->post('harsat_ket');
        $harsat_satuan = $this->input->post('harsat_satuan');
        $harsat_nilai = $this->input->post('harsat_nilai');
        
        if ( $harsat_ket != ""){
                
            if (! is_array($harsat_ket)){
                $harsat_ket = array($harsat_ket);
                $harsat_satuan = array($harsat_satuan);
                $harsat_nilai = array($harsat_nilai);
            }
            
            for($i=0;$i<count($harsat_ket);$i++){
            
                $data = array(
                    't_monitor_id' => $monitor_id, 
                    'harsat_ket' => $harsat_ket[$i], 
                    'harsat_satuan' => $harsat_satuan[$i], 
                    'harsat_nilai' => $harsat_nilai[$i], 
                      
                    'harsat_insert_user'	=>$_SESSION[SESSION_USERID],
                    'harsat_insert_date'=> date("Y-m-d h:i:s"));
                $this->m_harsat->_insert($data);
            }
        }
    }
    
    function _deletedetail($monitor_id){
        $this->m_harsat->_deletebymonitor($monitor_id);
    }
    
    function _detail(){
        $monitor_id = secure_post("monitor_id");
        echo json_encode($this->m_harsat->_listbymonitor($monitor_id));
    }
}